<?php
declare(strict_types=1);
/*
供应商送货单
*/
namespace Modules\Purchasing\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Purchasing\Http\Repositories\SupplierReget;
use Illuminate\Http\Request;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Supplierschedule;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Purchasing\Models\Purchaseorder;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Purchasing\Models\Barcodeprinting;
use Modules\Purchasing\Models\Suppliernamedelivery;
use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;   
use Catch\Exceptions\FailedException;
use Modules\Purchasing\Models\Provisional;
use Modules\Purchasing\Models\Provisionalschedule;
use Modules\Warehouse\Models\Storagelocation;
use Modules\Purchasing\Models\Inspectionlisttable;
use Modules\Purchasing\Models\Purchasewarehousing;
use Modules\Systemsetting\Models\Systemsetting;


class SupplierController extends Controller
{
   
    public function __construct(
        protected readonly Supplier $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
       
        $data = SupplierReget::list($request->all());
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
        // p($data);
        foreach($data as $k=>$v){
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
            //采购入库单
            $fbillno = '';
            $fpurchaseorgid_name = '';
            $purchaseorder_id = [];
            foreach($v['detailscheckAll'] as $kk=>$vv){
                $purchaseorder_id[] = $vv['purchaseorder_id'];
            }
            $purchaseorder_id = array_unique($purchaseorder_id);
            $purchaseorder = purchaseorder::select('fbillno','id','fpurchaseorgid_number','fpurchaseorgid_name')
                ->whereIn('id',$purchaseorder_id)
                ->get()->toArray();
            if(!empty($purchaseorder)){
                foreach ($purchaseorder as $ks=>$vs){
                    $fbillno .= $vs['fbillno'].'、';
                    $fpurchaseorgid_name .= $vs['fpurchaseorgid_name'].'、';
                }
                
            } 
            $fmaterialidName = rtrim($fbillno,'、');
            $explode = explode('、',$fmaterialidName);
            $explode = array_unique($explode);
            $fbillnodata='';
            foreach ($explode as $k1=>$v1){
                $res = mb_convert_encoding($v1, 'UTF-8', 'UTF-8'); 
                $fbillnodata .=$res .'、';
            }
            $data[$k]['fbillno'] = rtrim($fbillnodata,'、');

            $fpurchaseorgid_name = rtrim($fpurchaseorgid_name,'、');
            $fpurchaseorgid_name = explode('、',$fpurchaseorgid_name);
            $fpurchaseorgid_name = array_unique($fpurchaseorgid_name);

            $fpurchaseorgid_name_data = '';
              foreach ($fpurchaseorgid_name as $k1=>$v1){
                $res = mb_convert_encoding($v1, 'UTF-8', 'UTF-8'); 
                $fpurchaseorgid_name_data .=$res .'、';
            }
            $data[$k]['fpurchaseorgid_name'] = rtrim($fpurchaseorgid_name_data,'、');



           
            if($data[$k]['temporary']==1){
                $data[$k]['temporary'] = '已暂收';
            }else{
                $data[$k]['temporary'] = '未暂收';
            }
            
            switch ($v['state']){
                case 0:
                    $data[$k]['description'] = '待审核'; 
                break;
                case 1:
                    $data[$k]['description'] = '已审核'; 
                break;
            }
        }
        
        return $data;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $data['creator_id'] =$this->getLoginUserId();//当前登录用户名称
        return SupplierReget::create($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
      
        $data = SupplierReget::find($id);
        $Provisionalschedule = Provisionalschedule::where('supplier_id',$id)->first();
        if(!empty($Provisionalschedule)){
            $data['temporary'] = '已暂收';
        }else{
            $data['temporary'] = '未暂收';
        }
        
       
         
        foreach($data['detailscheckAll'] as $k=>$v){
            $res = Purchaseorder::find($v['purchaseorder_id']);
            $data['detailscheckAll'][$k]['fbillno'] = $res['fbillno'];
            $data['detailscheckAll'][$k]['fsupplierid_number'] = $res['fsupplierid_number'];
            $data['detailscheckAll'][$k]['fsupplierid_name'] = $res['fsupplierid_name'];
            $data['detailscheckAll'][$k]['fprovidercontactid'] = $res['fprovidercontactid'];
            $data['detailscheckAll'][$k]['fproviderphone'] = $res['fproviderphone'];
            $datares = Purchaseorderschedule::where('id',$v['purchaseorderschedule_id'])->first();

            $data['detailscheckAll'][$k]['fentryid'] = $datares['fentryid'];
            $data['detailscheckAll'][$k]['fmaterialid'] = $datares['fmaterialid'];//品号
            $data['detailscheckAll'][$k]['fmaterialname'] = $datares['fmaterialname'];
            $data['detailscheckAll'][$k]['fmaterialmodel'] = $datares['fmaterialmodel'];
            $data['detailscheckAll'][$k]['funitid_name'] = $datares['funitid_name'];
            $data['detailscheckAll'][$k]['fqty'] = $datares['fqty'];
            // $data['detailscheckAll'][$k]['fqty'] = $res['fqty'];
            // $data['detailscheckAll'][$k]['deliverydate'] = date('Y-m-d',$v['deliverydate']);
        }
        return  $data;
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id,Request $request)
    {   
        $data = $request->all();
        $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
        return SupplierReget::createUpdate($id,$data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return SupplierReget::delete($id);
    }

    /**审核接口
     * @param $id
     * @return mixed
     */
    public function auditupdateApi($id)
    {
        
        $data = [];
        $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
  
        return SupplierReget::auditupdate($id,$data);
    }

    /*条码列表接口
     * @param $id
     * @return mixed
     */
    public function barcodeApi($id,Request $request)
    {
        $data = $request->all();
        $data['supplier_id'] = $id;
        // $dataList = Barcodeprinting::where('supplier_id',$id)->get();
        $dataList = SupplierReget::listTwo($data);
        foreach($dataList as $k=>$v){
            $res = Supplier::find($v['supplier_id']);
            $dataList[$k]['supplierOrder']  =  $res['supplierOrder'];
            $resdat = Purchaseorderschedule::where('id',$v['purchaseorderschedule_id'])->first();
            $respurchaseorder = purchaseorder::find($resdat['purchaseorder_id']);
            $dataList[$k]['fmaterialid']  =  $resdat['fmaterialid'];
            $dataList[$k]['fmaterialname']  =  $resdat['fmaterialname'];
            $dataList[$k]['fbillno']  =  $respurchaseorder['fbillno'];
            $dataList[$k]['fsupplierid_number']  =  $respurchaseorder['fsupplierid_number'];
        }
        return $dataList;
    }


    /*条码箱号打印接口
     * @param $id
     * @return mixed
     */
    public function barcodePrinting(Request $request)
    {
       
        $kingDeeService = new warehouseService();
        $data = $request->all();
        $dataList = Barcodeprinting::whereIn('barcode',$data['barcode'])->get()->toArray();
    
        foreach($dataList as $k=>$v){
            // $res = Supplier::find($v['supplier_id']);
           $res = Supplierschedule::query()
            ->select('sp.supplierOrder','supplierschedule.quantity','supplierschedule.deliverydate','supplierschedule.number')
            ->join('supplier as sp', 'sp.id', '=', 'supplierschedule.supplier_id')
            // ->where('supplierschedule.id',$v['supplierschedule_id'])
            ->where('supplierschedule.supplier_id',$v['supplier_id'])
            ->where('supplierschedule.purchaseorderschedule_id',$v['purchaseorderschedule_id'])
            ->first();
            $dataList[$k]['supplierOrder']  =  $res['supplierOrder'];
            $dataList[$k]['quantity']  =  $res['quantity'];
            $dataList[$k]['deliverydate']  =  date('Y-m-d',strtotime($res['deliverydate']));
            $dataList[$k]['number']  =  $res['number'];
           
            $resdat = Purchaseorderschedule::select(
                'purchaseorder.fbillno',
                'purchaseorder.fsupplierid_number',
                'purchaseorderschedule.fmaterialid',
                'purchaseorderschedule.fmaterialname'
                )
            ->join('purchaseorder as purchaseorder', 'purchaseorder.id', '=', 'purchaseorderschedule.purchaseorder_id')
            ->where('purchaseorderschedule.id',$v['purchaseorderschedule_id'])
            ->first();
            // $respurchaseorder = purchaseorder::find($resdat['purchaseorder_id']);
            $dataList[$k]['fmaterialid']  =  $resdat['fmaterialid'];
            $dataList[$k]['fmaterialname']  =  $resdat['fmaterialname'];
            $dataList[$k]['fbillno']  =  $resdat['fbillno'];
            $dataList[$k]['fsupplierid_number']  =  $resdat['fsupplierid_number'];
           
            $data = $kingDeeService->createQrCode('100',$v['barcode'],$v['barcode']);
            $Twocode = env('APP_URL').'/uploads/barcode/'.$v['barcode'].'.png';
            
            $Twocodeimg = file_get_contents($Twocode);
            $imglstype  = pathinfo($Twocode,PATHINFO_EXTENSION);
            $dataList[$k]['Twocode']  = "data:image/$imglstype;base64," . base64_encode($Twocodeimg);

            $str = $resdat['fmaterialid'];
            $pattern = "/[^A-Za-z0-9]/"; // 匹配除了字母和数字之外的所有字符
            $replacement = ""; // 替换为空字符串
            $str = preg_replace($pattern, $replacement, trim($str));
            $data = $kingDeeService->createQrCode('100',$resdat['fmaterialid'],$str);

            // $dataList[$k]['TwocodefMaterialid']  = env('APP_URL').'/uploads/barcode/'.$resdat['fmaterialid'].'.png';
            $TwocodefMaterialid = env('APP_URL').'/uploads/barcode/'.$str.'.png';
            
            
            $img = file_get_contents($TwocodefMaterialid);
            $imgltwo  = pathinfo($TwocodefMaterialid,PATHINFO_EXTENSION);
            $dataList[$k]['TwocodefMaterialid']  = "data:image/$imgltwo;base64," . base64_encode($img);

        } 
      
        return $dataList;
        
    }
    
    
    
    /*条码箱号打印接口
     * @param $id
     * @return mixed
     */
    public function codePdf(Request $request)
    {
        
        
         $kingDeeService = new warehouseService();
        $data = $request->all();
        $dataList = Barcodeprinting::whereIn('barcode',$data['barcode'])->get()->toArray();
    
        foreach($dataList as $k=>$v){
            // $res = Supplier::find($v['supplier_id']);
           $res = Supplierschedule::query()
            ->select('sp.supplierOrder','supplierschedule.quantity','supplierschedule.deliverydate','supplierschedule.number')
            ->join('supplier as sp', 'sp.id', '=', 'supplierschedule.supplier_id')
            // ->where('supplierschedule.id',$v['supplierschedule_id'])
            ->where('supplierschedule.supplier_id',$v['supplier_id'])
            ->where('supplierschedule.purchaseorderschedule_id',$v['purchaseorderschedule_id'])
            ->first();
            $dataList[$k]['supplierOrder']  =  $res['supplierOrder'];
            $dataList[$k]['quantity']  =  $res['quantity'];
            $dataList[$k]['deliverydate']  =  date('Y-m-d',strtotime($res['deliverydate']));
            $dataList[$k]['number']  =  $res['number'];
           
            $resdat = Purchaseorderschedule::select(
                'purchaseorder.fbillno',
                'purchaseorder.fsupplierid_number',
                'purchaseorderschedule.fmaterialid',
                'purchaseorderschedule.fmaterialname'
                )
            ->join('purchaseorder as purchaseorder', 'purchaseorder.id', '=', 'purchaseorderschedule.purchaseorder_id')
            ->where('purchaseorderschedule.id',$v['purchaseorderschedule_id'])
            ->first();
            // $respurchaseorder = purchaseorder::find($resdat['purchaseorder_id']);
            $dataList[$k]['fmaterialid']  =  $resdat['fmaterialid'];
            $dataList[$k]['fmaterialname']  =  $resdat['fmaterialname'];
            $dataList[$k]['fbillno']  =  $resdat['fbillno'];
            $dataList[$k]['fsupplierid_number']  =  $resdat['fsupplierid_number'];
           
            $data = $kingDeeService->createQrCode('100',$v['barcode'],$v['barcode']);
            $Twocode = env('APP_URL').'/uploads/barcode/'.$v['barcode'].'.png';
              $dataList[$k]['Twocode']  = $Twocode;
              
            // $Twocodeimg = file_get_contents($Twocode);
            // $imglstype  = pathinfo($Twocode,PATHINFO_EXTENSION);
            // $dataList[$k]['Twocode']  = "data:image/$imglstype;base64," . base64_encode($Twocodeimg);

            $str = $resdat['fmaterialid'];
            $pattern = "/[^A-Za-z0-9]/"; // 匹配除了字母和数字之外的所有字符
            $replacement = ""; // 替换为空字符串
            $str = preg_replace($pattern, $replacement, trim($str));
            $data = $kingDeeService->createQrCode('100',$resdat['fmaterialid'],$str);

            // $dataList[$k]['TwocodefMaterialid']  = env('APP_URL').'/uploads/barcode/'.$resdat['fmaterialid'].'.png';
            $TwocodefMaterialid = env('APP_URL').'/uploads/barcode/'.$str.'.png';
            $dataList[$k]['TwocodefMaterialid'] = $TwocodefMaterialid;
            
            // $img = file_get_contents($TwocodefMaterialid);
            // $imgltwo  = pathinfo($TwocodefMaterialid,PATHINFO_EXTENSION);
            // $dataList[$k]['TwocodefMaterialid']  = "data:image/$imgltwo;base64," . base64_encode($img);
        } 
        
        return $this->Pdf($dataList);
    }

    /* 全部导出
     * @param $id
     * @return mixed
     */
    public function codePdfall($id)
    {
       
        
        $kingDeeService = new warehouseService();
        // $data = $request->all();
        // $data = explode(',',);
        $dataList = Barcodeprinting::where('supplier_id',$id)->get()->toArray();
        // $dataList = Barcodeprinting::whereIn('barcode',$data['barcode'])->get()->toArray();
       
        foreach($dataList as $k=>$v){
            // $res = Supplier::find($v['supplier_id']);
           $res = Supplierschedule::query()
            ->select('sp.supplierOrder','supplierschedule.quantity','supplierschedule.deliverydate','supplierschedule.number')
            ->join('supplier as sp', 'sp.id', '=', 'supplierschedule.supplier_id')
            // ->where('supplierschedule.id',$v['supplierschedule_id'])
            ->where('supplierschedule.supplier_id',$v['supplier_id'])
            ->where('supplierschedule.purchaseorderschedule_id',$v['purchaseorderschedule_id'])
            ->first();
            $dataList[$k]['supplierOrder']  =  $res['supplierOrder'];
            $dataList[$k]['quantity']  =  $res['quantity'];
            $dataList[$k]['deliverydate']  =  date('Y-m-d',strtotime($res['deliverydate']));
            $dataList[$k]['number']  =  $res['number'];
           
            $resdat = Purchaseorderschedule::select(
                'purchaseorder.fbillno',
                'purchaseorder.fsupplierid_number',
                'purchaseorderschedule.fmaterialid',
                'purchaseorderschedule.fmaterialname'
                )
            ->join('purchaseorder as purchaseorder', 'purchaseorder.id', '=', 'purchaseorderschedule.purchaseorder_id')
            ->where('purchaseorderschedule.id',$v['purchaseorderschedule_id'])
            ->first();
            // $respurchaseorder = purchaseorder::find($resdat['purchaseorder_id']);
            $dataList[$k]['fmaterialid']  =  $resdat['fmaterialid'];
            $dataList[$k]['fmaterialname']  =  $resdat['fmaterialname'];
            $dataList[$k]['fbillno']  =  $resdat['fbillno'];
            $dataList[$k]['fsupplierid_number']  =  $resdat['fsupplierid_number'];
           
            $data = $kingDeeService->createQrCode('100',$v['barcode'],$v['barcode']);
            $Twocode = env('APP_URL').'/uploads/barcode/'.$v['barcode'].'.png';
              $dataList[$k]['Twocode']  = $Twocode;
              
            // $Twocodeimg = file_get_contents($Twocode);
            // $imglstype  = pathinfo($Twocode,PATHINFO_EXTENSION);
            // $dataList[$k]['Twocode']  = "data:image/$imglstype;base64," . base64_encode($Twocodeimg);

            $str = $resdat['fmaterialid'];
            $pattern = "/[^A-Za-z0-9]/"; // 匹配除了字母和数字之外的所有字符
            $replacement = ""; // 替换为空字符串
            $str = preg_replace($pattern, $replacement, trim($str));
            $data = $kingDeeService->createQrCode('100',$resdat['fmaterialid'],$str);

            // $dataList[$k]['TwocodefMaterialid']  = env('APP_URL').'/uploads/barcode/'.$resdat['fmaterialid'].'.png';
            $TwocodefMaterialid = env('APP_URL').'/uploads/barcode/'.$str.'.png';
            $dataList[$k]['TwocodefMaterialid'] = $TwocodefMaterialid;
            
            // $img = file_get_contents($TwocodefMaterialid);
            // $imgltwo  = pathinfo($TwocodefMaterialid,PATHINFO_EXTENSION);
            // $dataList[$k]['TwocodefMaterialid']  = "data:image/$imgltwo;base64," . base64_encode($img);
        } 
        return $this->Pdf($dataList,'all');
      
     

    }


    /*pdf 导出方法
     */
    public function Pdf($data,$type=null){

        $pageLayout = array(500, 500);
        $pdf = new \TCPDF('p', 'pt', $pageLayout, true, 'UTF-8', false);

        // 设置文档信息
        $pdf->SetCreator('懒人开发网');
        $pdf->SetAuthor('懒人开发网');
        $pdf->SetTitle('TCPDF示例');
        $pdf->SetSubject('TCPDF示例');
        $pdf->SetKeywords('TCPDF, PDF, PHP');

        // 设置页眉和页脚信息
        // $pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'LanRenKaiFA.com', '学会偷懒，并懒出效率！', [0, 64, 255], [0, 64, 128]);
        $pdf->setFooterData([0, 64, 0], [0, 64, 128]);

        // 设置页眉和页脚字体
        $pdf->setHeaderFont(['stsongstdlight', '', '10']);
        $pdf->setFooterFont(['helvetica', '', '8']);

        // 设置默认等宽字体
        $pdf->SetDefaultMonospacedFont('courier');

        // 设置间距
        $pdf->SetMargins(15, 15, 15);//页面间隔
        $pdf->SetHeaderMargin(5);//页眉top间隔
        $pdf->SetFooterMargin(10);//页脚bottom间隔

        // 设置分页
        // $pdf->SetAutoPageBreak(true, 25);
        $pdf->SetAutoPageBreak(false);
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        //设置字体 stsongstdlight支持中文
        $pdf->SetFont('stsongstdlight', '', 14);
      
        $supplierOrder = '';
    foreach ($data as $k=>$v){
        $supplierOrder = $v['supplierOrder'];
        $pdf->AddPage();
        $quantity = floatval($v['quantity']);
        //  {$v['barcode']}
        $tbl = <<<EOD

        <table border="2" cellpadding="5" >
        <tr >
            <td style="text-align: center;" rowspan="2"> <img src="{$v['Twocode']}" /></td>
            <td style="text-align: center;height:40px;line-height:40px;font-weight:bold;font-size:18px;">箱号:</td>
            <td style="text-align: center;height:40px;line-height:40px;font-weight:bold;font-size:18px;" colspan="2">{$v['barcode']}</td>
        </tr>
        <tr>
            <td style="text-align: center;line-height:50px;font-weight:bold;font-size:18px;">供应商单号:</td>
            <td style="text-align: center;line-height:50px;font-weight:bold;font-size:18px;" colspan="2">{$v['supplierOrder']}</td>
        </tr>
        <tr>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;" >采购订单：</td>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;"  colspan="3" > {$v['fbillno']}</td>
        </tr>
        <tr>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;" >存货全名：</td>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;"  colspan="3"> {$v['fmaterialname']}</td>
        </tr>
        <tr>
            <td style="text-align: center;line-height:30px; font-weight:bold;font-size:18px;">存货编码：</td>
            <td style="text-align: center;line-height:30px;font-weight:bold;font-size:18px;" colspan="2" >{$v['fmaterialid']} </td>
                <td style="text-align: center;line-height:30px;font-weight:bold;font-size:18px;"> 数量 : {$quantity}</td>
        
        </tr>
        
        <tr>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;" >本箱数量：</td>
            <td style="text-align: center;height:30px;line-height:30px;" ></td>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;" >个</td>
            <td style="text-align: center;height:20px;" rowspan="2"><img src="{$v['TwocodefMaterialid']}"/></td>
        </tr>
        <tr>
            <td style="text-align: center;height:40px;line-height:80px;font-weight:bold;font-size:18px;">发货日期：</td>
            <td style="text-align: center;height:40px;line-height:80px;font-weight:bold;font-size:18px;" colspan="2"> {$v['deliverydate']}</td>
        </tr>
        <tr>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;">生产日期：</td>
            <td ></td>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;">有效日期：</td>
            <td style="text-align: center;height:30px;line-height:30px;" ></td>
        </tr>
        <tr>
            <td style="text-align: center;height:30px;line-height:30px;font-weight:bold;font-size:18px;">备注：</td>
            <td colspan="3" style="height:30px;line-height:30px;"></td>
        </tr>
        
        
        </table>
        EOD;
        $pdf->writeHTML($tbl, true, false, false, false, '');
    
            
        }
        
     

       //第一页
       // $pdf->writeHTML('<div style="text-align: center"><h1>第一页内容</h1></div>');
       // $pdf->writeHTML('<p>我是第一行内容</p>');
       // $pdf->writeHTML('<p style="color: red">我是第二行内容</p>');
       // $pdf->writeHTML('<p>我是第三行内容</p>');
       // $pdf->Ln(5);//换行符
       // $pdf->writeHTML('<p><a href="http://www.lanrenkaifa.com/" title="">懒人开发网</a></p>');

       // //第二页
       // $pdf->AddPage();
       // $pdf->writeHTML('<h1>第二页内容5555</h1>');

       //输出PDF 四种模式 I输出、D下载、F保存本地、S输出二进制字符串
       //  $pdf->Output('t.pdf', 'D');//I输出、D下载 FI
       //请求下载
       
       
        try {
            if($type){
                $name     = $supplierOrder.'all.pdf';
            }else{
                $name     = $supplierOrder.'.pdf';
            }
           $fileName = $_SERVER['DOCUMENT_ROOT'].'/uploads/pdf/'.$name;
         
           $pdf->Output($fileName, 'F');
           $downUrl = env('APP_URL').'/uploads/pdf/'.$name;
          return ['pdfUrl'=>$downUrl];
        } catch (\Exception $e) {
           return $e;
           throw $e;
       }
        // $downUrl = env('APP_URL').'/uploads/pdf/'.$name;
        // header("Content-type:application/pdf");
        // header("Content-Disposition:attachment;filename=$downUrl");
        // readfile($downUrl);

        // header('Content-Type:application/force-download');
        // header('Content-Disposition: attachment;filename="'.$name.'"');
        // readfile($fileName);
        // unlink($fileName);

    }
   
    /*品名列表接口
     * 
     * @return mixed
     */
     public function categoryApi($id)
     {

        $dataList = Suppliernamedelivery::where('supplier_id',$id)->get();

         foreach($dataList as $k=>$v){
             $res = Purchaseorderschedule::where('id',$v['purchaseorderschedule_id'])->first();
             if(!empty($res)){
                $dataList[$k]['fseqname']  =  $res['fmaterialid'];
             }else{
                $dataList[$k]['fseqname']  =  '';
             }
         }
         return $dataList;
     }
 

    /**生成品名数据
     * @param $id
     * @return mixed
     */
    // public function Postcategory(Request $request)
    // {
    //     $data = $request->all(); 
    //     return SupplierReget::PostcategoryData($data);
    // }

    // /*品名修改接口
    //  * @param $id
    //  * @return mixed
    //  */
    // public function Postcategorymodify($id,Request $request)
    // {

    //     $data = $request->all(); 
    //     $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
    //     return SupplierReget::PostcategoryDatamodify($id,$data);
    // }


    /*PDA 扫描接口
     * @param $id
     * @return mixed
     */
    public function barcodeApiscan($barcode)
    { 
    
        // $storagelocation = Db::table('barcodeprinting')->select('id')->where('barcode','=',$barcode)->first();
        // if(!empty($storagelocation)){
            //     $data = [
            //         'code'=>1000,
            //         'message'=>'已扫描无法在次生成'
            //     ];
            //     return $data;
        // }

        $dataList = Barcodeprinting::where('barcode',$barcode)->where('void',0)->first();
      
        if(empty($dataList)){
            return  throw new FailedException('亲，该送货箱号不存在，请联系采购人员处理哦');
        }

        $Provisionalschedule = Provisionalschedule::where('barcode',$barcode)->first();

        if(empty(!$Provisionalschedule)){

            return  throw new FailedException('亲，已暂收过无法在暂收哦');
        }
        
        try {
            //开始起事务
            DB::beginTransaction();
                $dataList = Barcodeprinting::where('barcode',$barcode)->first();
      
                $supplierOrder = Supplier::where('id',$dataList['supplier_id'])->first();
                $dataList['supplierOrder'] = $supplierOrder['supplierOrder'];//送货单
         
                $Purchaseorderschedule =  Purchaseorderschedule::
                where('id',$dataList['purchaseorderschedule_id'])
                ->first();
                
                if($Purchaseorderschedule['fpurchaseorgid_number']==106&&$Purchaseorderschedule['fsupplierid_number']=='VEN00721'){
                    $InterorgOpening = Systemsetting::Opening('*',['argument_code'=>'InterorgOpening']);
                    //组织间关闭
                    if(!empty($InterorgOpening)){
                        return throw new FailedException('亲，组织间暂收已关闭流程，可直接扫描入库');
                    }        
                }
             
                $dataList['fmaterialid'] = $Purchaseorderschedule['fmaterialid']; 
                $dataList['fmaterialname'] = $Purchaseorderschedule['fmaterialname']; 
                $dataList['fmaterialmodel'] = $Purchaseorderschedule['fmaterialmodel'];
                $dataList['fqty'] = $Purchaseorderschedule['fqty'];   //采购数量
                $dataList['fseq'] = $Purchaseorderschedule['fseq'];   //行号
                // $quantity =  Supplierschedule::select(DB::raw('SUM(quantity) as quantity'),'supplier_id','purchaseorderschedule_id','purchaseorder_id','number')
                // ->where('purchaseorderschedule_id',$dataList['purchaseorderschedule_id'])
                // ->where('supplier_id',$dataList['supplier_id'])
                // ->groupby(['supplier_id','purchaseorderschedule_id','purchaseorder_id']) 
                // ->first();
                $quantity =  Supplierschedule::select('quantity','supplier_id','purchaseorderschedule_id','purchaseorder_id','number','id')
                ->where('purchaseorderschedule_id',$dataList['purchaseorderschedule_id'])
                ->where('supplier_id',$dataList['supplier_id'])
                // ->groupby(['supplier_id','purchaseorderschedule_id','purchaseorder_id']) 
                ->first();
                

                $dataList['number'] = $quantity['number']; //箱数
              
                $dataList['quantity'] = $quantity['quantity']; //送货数量
                $dataList['purchaseorder_id'] = $quantity['purchaseorder_id'];   //采购订单主表
                $dataList['purchaseorderschedule_id'] = $quantity['purchaseorderschedule_id']; //物料id

                $Purchaseorder =  Purchaseorder::where('id',$quantity['purchaseorder_id'])->first();
                $dataList['fbillno'] = $Purchaseorder['fbillno'];   //采购单号
                $dataList['fsupplierid_number'] = $Purchaseorder['fsupplierid_number'];   //供应编码
                $dataList['fsupplierid_name'] = $Purchaseorder['fsupplierid_name'];   //供应名称

                //同个物料汇总总箱数
                $collect =  Supplierschedule::select('quantity','supplier_id','purchaseorderschedule_id','purchaseorder_id','number','id')
                ->where('supplier_id',$dataList['supplier_id'])
                // ->groupby(['supplier_id','purchaseorderschedule_id','purchaseorder_id']) 
                ->get()
                ->toArray();
                $purchaseorderschedule_id = [];  
                foreach($collect as $k=>$v){
                    $purchaseorderschedule_id[] = $v['purchaseorderschedule_id'];
                }

                $Purchaseorderschedule =  Purchaseorderschedule::select('id')
                ->whereIn('id',$purchaseorderschedule_id)
                ->where('fmaterialid',$Purchaseorderschedule['fmaterialid'])
                ->where('fmaterialname',$Purchaseorderschedule['fmaterialname'])
                ->where('fmaterialmodel',$Purchaseorderschedule['fmaterialmodel'])
                ->get()
                ->toArray();
                $purchaseorderschedule_id_two = [];
                foreach($Purchaseorderschedule  as $k=>$v){
                    $purchaseorderschedule_id_two[] = $v['id']; 
                }
                $collect =  Supplierschedule::select('number')
                ->where('supplier_id',$dataList['supplier_id'])
                ->whereIn('purchaseorderschedule_id',$purchaseorderschedule_id_two)
                ->get()->toArray();
                $number = 0; 
                foreach ($collect as $k=>$v){
                    $number +=$v['number'];  
                }
              

                $dataList['sinktotal'] = $number; //汇总箱数
               
                // $Supplierschedule_id =  Supplierschedule::
                // select('id','purchaseorderschedule_id')
                // ->where('purchaseorderschedule_id',$dataList['purchaseorderschedule_id'])
                // ->where('supplier_id',$dataList['supplier_id'])
                // ->first();
                
                // $Supplierschedule_id_data = [];
                // $purchaseorderschedule_id = [];
                // foreach($Supplierschedule_id as $k=>$v){
                //     $Supplierschedule_id_data[] = $v['id'];
                //     $purchaseorderschedule_id[] = $v['purchaseorderschedule_id'];
                // }
                
                $dataList['supplierschedule_id'] = $quantity['id'];   
                
               
            DB::commit();
            return $dataList;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
}

    /*供应商条码作废接口 条码列表接口
     * @param $id
     * @return mixed
     */
public function barcodevoid(Request $request)
    {
        $data = $request->all();
        if(empty($data)){
            return throw new FailedException('提交的数据不能为空');
        }
        try {
            //开始起事务
            DB::beginTransaction();
            foreach($data as $k=>$v){
                Barcodeprinting::query()->where('id',$v['id'])->update(
                    ['void'=>$v['void']]
                );
            }
            DB::commit();
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            return throw '数据出错请联系技术处理';
        }
        
    }

    /*供应商条码作废接口 条码列表接口
     * @param $id
     * @return mixed
     */
    public function modifydataapi(Request $request)
    {
        $data = $request->all();
        return SupplierReget::modifydata($data);
        
      
    }

  



}
